/**************************************************
* File name: test_demo.c
* Author: HAN Wei
* Author's blog: https://blog.csdn.net/henter/
* Date: Dec 9th, 2018
* Description: implement test demo program for
    SM2 encrypt data and decrypt ciphertext
**************************************************/

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

#include <openssl/obj_mac.h>
#include <openssl/evp.h>
#include <openssl/bn.h>
#include <openssl/ec.h>


#include "test_sm2_encrypt_and_decrypt.h"
#include "sm2_cipher_error_codes.h"
#include "sm2_create_key_pair.h"
#include "sm2_encrypt_and_decrypt.h"
//#include "w_sm2.h"

/*********************************************************/
int main(void)
{
	int error_code;
	unsigned char msg[] = { "woshinibaba" };
//wxyg++
	//int msg_len = (int)(strlen((char*)msg));
int msg_len = 11;
	SM2_KEY_PAIR key_pair;
	unsigned char c1[65], c3[32];
	unsigned char* c2, * plaintext;
	int i;

	if (error_code = sm2_create_key_pair(&key_pair))
	{
		printf("Create SM2 key pair failed!\n");
		return (-1);
	}

//wxyg++
	//printf("Create SM2 key pair succeeded!  %d \n", strlen((const char*)key_pair.pri_key));
	printf("Private key: %s \n", key_pair.pri_key);
	for (i = 0; i < sizeof(key_pair.pri_key); i++)
	{
		printf("0x%x  ", key_pair.pri_key[i]);
	}
	printf("\n\n");
	printf("Public key: %s \n", key_pair.pub_key);
	for (i = 0; i < sizeof(key_pair.pub_key); i++)
	{
		printf("0x%x  ", key_pair.pub_key[i]);
	}
	printf("\n\n");

	printf("/*********************************************************/\n");
	if (!(c2 = (unsigned char*)malloc(msg_len)))
	{
		printf("Memory allocation failed!\n");
		return ALLOCATION_MEMORY_FAIL;
	}
	if (error_code = sm2_encrypt_data_test(msg,
		msg_len,
		key_pair.pub_key,
		c1,
		c3,
		c2))
	{
		printf("Create SM2 ciphertext failed!\n");
		free(c2);
		return error_code;
	}

	printf("Create SM2 ciphertext succeeded!\n");
	printf("SM2 ciphertext:\n\n");
	printf("C1 component:\n");
	for (i = 0; i < sizeof(c1); i++)
	{
		printf("0x%x  ", c1[i]);
	}
	printf("\n\n");
	printf("C3 component:\n");
	for (i = 0; i < sizeof(c3); i++)
	{
		printf("0x%x  ", c3[i]);
	}
	printf("\n\n");
	printf("Message: %s\n", msg);
	printf("The length of message is %d bytes.\n", msg_len);
	printf("The length of C2 component is %d bytes.\n", msg_len);
	printf("C2 component:\n");
	for (i = 0; i < msg_len; i++)
	{
		printf("0x%x  ", c2[i]);
	}
	printf("\n\n");

	if (!(plaintext = (unsigned char*)malloc(msg_len)))
	{
		printf("Memory allocation failed!\n");
		return ALLOCATION_MEMORY_FAIL;
	}

	if (error_code = sm2_decrypt(c1,
		c3,
		c2,
		msg_len,
		key_pair.pri_key,
		plaintext))
	{
		printf("plaintext %s \n", plaintext);
		free(plaintext);
		free(c2);
		printf("Decrypt SM2 ciphertext failed!\n");
		return error_code;
	}
//wxyg++
printf("plaintext %s \n", plaintext);
	//if (memcmp(plaintext, msg, msg_len))
	{
		//printf("Decrypted plaintext is different from the input message!\n");
		//return SM2_DECRYPT_FAIL;
	}
	printf("Input message:\n");
	for (i = 0; i < msg_len; i++)
	{
		printf("0x%x  ", msg[i]);
	}
	printf("\n");
	printf("Decrypted message:\n");
	for (i = 0; i < msg_len; i++)
	{
		printf("0x%x  ", plaintext[i]);
	}
	printf("\n");
	printf("Decrypt SM2 ciphertext succeeded!\n");

	free(plaintext);
	free(c2);













	////w_sm2_create_key_pair

	//int error_code;
	//unsigned char msg[] = { "encryption standard" };
	//int msg_len = (int)(strlen((char*)msg));
	//SM2_KEY_PAIR key_pair;
	//unsigned char c1[65], c3[32];
	//unsigned char* c2, * plaintext;
	//int i;

	////if (error_code = w_sm2_create_key_pair(&key_pair))
	////{
	////	printf("Create SM2 key pair failed!\n");

	////}
	///*std::cout << "hello wxy"<< error_code <<"\n";*/
	////int error_code;

	////printf("/*********************************************************/\n");
	////if ( error_code = w_test_with_input_defined_in_standard() )
	////{
	////	printf("Test SM2 encrypt data and decrypt ciphertext with input defined in standard failed!\n");
	////	return error_code;
	////}
	////else
	////{
	////	printf("Test SM2 encrypt data and decrypt ciphertext with input defined in standard succeeded!\n");
	////}

	//printf("\n/*********************************************************/\n");
	//if ( error_code = w_test_sm2_encrypt_and_decrypt() )
	//{
	//	printf("Test create SM2 key pair, encrypt data and decrypt ciphertext failed!\n");
	//	return error_code;
	//}
	//else
	//{
	//	printf("Test create SM2 key pair, encrypt data and decrypt ciphertext succeeded!\n");
	//}

#if defined(_WIN32) || defined(_WIN64)
  system("pause");
#endif
	return 0;
}
